package com.wk.search.controller;

import com.wk.search.service.SearchHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;

@RestController
@EnableScheduling
public class SendController {

    @Autowired
    private SearchHouseService searchHouseService;

    @GetMapping(value = "/send")
    @ResponseBody
    @Scheduled(cron = "0 */5 * * * ?")
    public String sendEmail() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = now.withHour(1).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endTime = now.withHour(8).withMinute(0).withSecond(0).withNano(0);
        if (startTime.compareTo(now) <= 0 && endTime.compareTo(now) >= 0) {
            return "不提醒!";
        }
        try {
            searchHouseService.search();
            return "请求成功!";
        } catch (MailException | IOException ex) {
            System.err.println(ex.getMessage());
            return "请求失败!";
        }
    }

}
